package com.airlines.crewassignment.domain.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.airlines.crewassignment.entity.ValueForDate;

/**
 * @author xxxx
 * @date 2018年4月16日 
 * @description
 */
public class ValueForDateUtil {
	
	public static List<ValueForDate>  groupByMonth(List<ValueForDate> list){
		List<ValueForDate> groupList = new ArrayList<>();
		Set<Integer> set = new HashSet<>();
		for(ValueForDate v : list){
			set.add(v.getMonth());
		}
		
		for(Integer group : set){
			ValueForDate valueForGroup = new ValueForDate();
			valueForGroup.setMonth(group);
			int groupSum = 0;
			
			for(ValueForDate v : list){
				if(v.getMonth() == group){
					groupSum += v.getValue();
				}
			}
			valueForGroup.setValue(groupSum);
			
			groupList.add(valueForGroup);
		}
		
		return groupList;
	}
	
	
	public static List<ValueForDate>  groupByYear(List<ValueForDate> list){
		List<ValueForDate> groupList = new ArrayList<>();
		Set<Integer> set = new HashSet<>();
		for(ValueForDate v : list){
			set.add(v.getYear());
		}
		
		for(Integer group : set){
			ValueForDate valueForGroup = new ValueForDate();
			valueForGroup.setYear(group);
			int groupSum = 0;
			
			for(ValueForDate v : list){
				if(v.getYear() == group){
					groupSum += v.getValue();
				}
			}
			valueForGroup.setValue(groupSum);
			groupList.add(valueForGroup);
		}
		
		return groupList;
	}
	
	public static int getMaxValue(List<ValueForDate> list){
		int max = 0;
		for(ValueForDate v : list){
			if(v.getValue()>max){
				max = v.getValue();
			}
		}
		return max;
	}
}
